package com.malamute.core.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.malamute.core.common.Page;
import com.malamute.core.covert.DateTypeConverter;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * @Title:BaseAction.java
 * @Package com.malamute.core.action
 * @Description 基础的Action 封装方法,封装常用拦截器
 * @author AceGuan gjybst@163.com
 * @date 2012-1-17 上午12:56:28
 * @version v1.0
 */

public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware, SessionAware, Preparable, ModelDriven {
	// Request 参数，在excute中用来判断
	// 按照json返回
	protected Logger log = Logger.getLogger(this.getClass());
	public static final String JSON = "json";
	public static final String LIST = "list";
	public static final String FORM = "form";
	public static final String ACTION = "action";
	public static final String VIEW="view";
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Page page = new Page();
	protected Map session;
	protected Map searchParam = new HashMap<String, String>();// 查询参数

	private static final String PACKAGEHEADER = "package ";
	private static final String ACTIONCLASSEND = "action";
	private String actionName;// acetion name;
	private String nameSpace;// action namespace;

	protected String dateType = DateTypeConverter.DEFAULT_DATE_FORMAT;// 转换时间类型

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	/**
	 * @return
	 */
	public String getNameSpace() {
		String className = this.getClass().getName();
		String namespaceAndName = className.substring(className
				.indexOf("action.") + 7);
		if (namespaceAndName.indexOf(".") > 0) {
			nameSpace = "/"
					+ namespaceAndName.substring(0,
							namespaceAndName.indexOf("."));
		} else {
			nameSpace = "/";
		}

		return nameSpace;
	}

	public void setNameSpace(String nameSpace) {
		this.nameSpace = nameSpace;
	}

	public String getActionName() {
		int packageLength = this.getClass().getPackage().toString().length()
				- PACKAGEHEADER.length() + 1;
		String allName = this.getClass().getName();
		return allName.substring(packageLength,
				allName.length() - ACTIONCLASSEND.length()).toLowerCase();
	}

	public static void main(String[] args) {
		BaseAction b = new BaseAction();
		System.out.println(b.getNameSpace());
		// System.out.println(BaseAction.class.getName());
		// String className = BaseAction.class.getName();
		// String name1 = "com.malamute.core.action.BaseAction";
		// String name2 = "com.malamute.core.action.admin.BaseAction";
		// String nameSpaceAndAction1 = name1.substring(name1
		// .lastIndexOf("action") + 7);
		// String nameSpaceAndAction2 = name2.substring(name2
		// .lastIndexOf("action") + 7);
		//
		// System.out.println(name1.substring(name1.lastIndexOf("action.") +
		// 7));
		// System.out.println(name2.substring(name2.lastIndexOf("action") + 7));
		//
		// if (nameSpaceAndAction1.indexOf(".") > 0) {
		// System.out.println(nameSpaceAndAction1.substring(0,
		// nameSpaceAndAction1.indexOf(".")));
		// }
		// if (nameSpaceAndAction2.indexOf(".") > 0) {
		// System.out.println(nameSpaceAndAction2.substring(0,
		// nameSpaceAndAction2.indexOf(".")));
		// }
		// System.out.println(BaseAction.class.getPackage().toString());
		// System.out.println(BaseAction.class.getName());
		// System.out.println(BaseAction.class.getName().substring(BaseAction.class.getPackage().toString().length()-"package ".length()+1));
		// System.out.println(b.getActionName());
	}

	/**
	 * 获取searchParam
	 * 
	 * @return searchParam
	 */

	public Map getSearchParam() {
		return searchParam;
	}

	/**
	 * 设置searchParam
	 * 
	 * @param searchParam
	 */
	public void setSearchParam(Map searchParam) {
		this.searchParam = searchParam;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param response
	 * 
	 * @see
	 * org.apache.struts2.interceptor.ServletResponseAware#setServletResponse
	 * (javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param request
	 * 
	 * @see
	 * org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(
	 * javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * 获取request
	 * 
	 * @return request
	 */

	public HttpServletRequest getServletRequest() {
		return request;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @throws Exception
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		log.info("prepare method......");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param session
	 * 
	 * @see
	 * org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
	 */
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;

	}

	/**
	 * 获取page
	 * 
	 * @return page
	 */

	public Page getPage() {
		return page;
	}

	/**
	 * 设置page
	 * 
	 * @param page
	 */
	public void setPage(Page page) {
		this.page = page;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	@Override
	public Object getModel() {
		return null;
	}

	public String getDateType() {
		return dateType;
	}

	public void setDateType(String dateType) {
		this.dateType = dateType;
	}

	
	
}
